package com.cloudlead.plugin.security.service.impl;

import com.cloudlead.common.lang.service.AbstractBaseService;
import com.cloudlead.plugin.security.BusinessModules;
import com.cloudlead.plugin.security.service.PasswordRetryService;
import com.cloudlead.utils.cache.CacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by Dean on 2015/4/14.
 */
@Service("PasswordRetryService")
public class PasswordRetryServiceImpl extends AbstractBaseService implements PasswordRetryService {

    public static final String PASSWORD_RETRY_CACHE = "security_password_retry_";
    public static final int PASSWORD_RETRY_EXP_SECONDS = 60; //3600;

    @Autowired
    private CacheService cacheService;

    public PasswordRetryServiceImpl() {
        super(BusinessModules.ScUser);
    }

    @Override
    public void addPasswordRetry(String principal, AtomicInteger retryCount) {
        cacheService.set(PASSWORD_RETRY_CACHE + principal, retryCount, PASSWORD_RETRY_EXP_SECONDS);
    }

    @Override
    public AtomicInteger getPasswordRetry(String principal) {
        return (AtomicInteger) cacheService.get(PASSWORD_RETRY_CACHE + principal);
    }

    @Override
    public void removePasswordRetry(String principal) {
        cacheService.delete(PASSWORD_RETRY_CACHE + principal);
    }
}
